from hexconfig import * 
import pygame

class edgeDictionary():
    def __init__(self):
        self.firstStrings = {}
        self.totalEdges = 0

    def keys(self):
        returnList = []
        for firstString in self.firstStrings.keys():
            #print firstString, decompressStateRLE(firstString)
            returnList.extend([decompressStateRLE(firstString)+decompressStateRLE(key) for key in self.firstStrings[firstString].keys()])
            #print [decompressStateRLE(key) for key in self.firstStrings[firstString].keys()]
        return returnList

    def getEdgesWithSameFirst(self, stateString):
        retlist = []
        cFirstString = compressStateRLE(stateString[:43])
        cSecondString = compressStateRLE(stateString[43:])
        try:
            for key in self.firstStrings[cFirstString].keys():
                retlist.append(self.firstStrings[cFirstString][cSecondString])
                
            try:
                self.firstStrings[cFirstString][cSecondString]
            except KeyError:
                print "node", stateString, "is node in the nodeDictionary FYI"

            return retlist
        except KeyError:
            print "nodeGroup", stateString, "doesn't exist"
            return []


    def __getitem__(self, key):
        try:
            #print compressStateRLE(key[:43]), compressStateRLE(key[43:])
            try:
                self.firstStrings[compressStateRLE(key[:43])]
            except KeyError:
                pass
                #print key[:43], compressStateRLE(key[:43])
                #print "whatttttttttttt?!?!?!!"

            return self.firstStrings[compressStateRLE(key[:43])][compressStateRLE(key[43:])]
        except KeyError:
            raise KeyError

    def __contains__(self, key):
        #print "trying to get", key
        try:
            self.firstStrings[compressStateRLE(key[:43])][compressStateRLE(key[43:])]
            #print "found", key
            return True
        except KeyError:
            #print "didn't find", key
            return False

    def __setitem__(self,key,value):
        cFirstString = compressStateRLE(key[:43])
        cSecondString = compressStateRLE(key[43:])
        try:
            firstStringDict = self.firstStrings[cFirstString]
        except KeyError:
            self.firstStrings[cFirstString] = {}
            firstStringDict = self.firstStrings[cFirstString]

        try:
            firstStringDict[cSecondString]
        except KeyError:
            self.totalEdges += 1

        firstStringDict[cSecondString] = value

    def __delitem__(self,key):
        try:
            del self.firstStrings[compressStateRLE(key[:43])][compressStateRLE(key[43:])]
            self.totalEdges -= 1
        except KeyError:
            raise KeyError

    def __len__(self):
        
        return self.totalEdges




if __name__ == "__main__":
    nd = nodeDictionary() #self.nodes
    stateStrings = ["-","A","B","1","0","1"]
    for s in stateStrings:
        nd[s] = StateNode(s) #self.nodes[s] = StateNode(s)

        #print s

    print nd.keys()
    for key in nd.keys():
        print nd[key]
    print len(nd)
    del nd["A"]
    for key in nd.keys():
        print nd[key]
    print len(nd)

